﻿// -------------------------------------------------------------------------
//    @FileName         :    NFCityPlayerEscortAgent.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-29
//    @Email			:    445267987@qq.com
//    @Module           :    NFCityPlayerEscortAgent
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFCityPlayerEscortAgent.h"

#include "ConstantDescEx.h"
#include "NFCityPlayer.h"
#include "proto_svr_common.nanopb.h"
#include "proto_svr_city.pb.h"
#include "DescStore/FunctionNpcDesc.h"
#include "NFCityServer/NFCityServerPlayerPlugin/Base/NFCityPacketSendUtil.h"

NFCityPlayerEscortAgent::NFCityPlayerEscortAgent()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFCityPlayerEscortAgent::~NFCityPlayerEscortAgent()
{
}

int NFCityPlayerEscortAgent::CreateInit()
{
    in_escort = false;
    in_battle = false;

    m_bChangeMap = false;
    m_iChangeMapMove = 0;

    m_iMove = 0;
    return 0;
}

int NFCityPlayerEscortAgent::ResumeInit()
{
    return 0;
}

void NFCityPlayerEscortAgent::SetEscortState(bool isInEscort)
{
    in_escort = isInEscort;
}

bool NFCityPlayerEscortAgent::GetEscortState() const
{
    return in_escort;
}

void NFCityPlayerEscortAgent::ClearEscortMove()
{
    m_iMove = 0;
}

void NFCityPlayerEscortAgent::SetBattleState(bool isInBattle)
{
    in_battle = isInBattle;
}

bool NFCityPlayerEscortAgent::GetBattleState() const
{
    return in_battle;
}

int CheckNearTeleports(int iMapDescID, const Proto_Vector3& oPos)
{
    for (int i = 0; i < FunctionNpcDesc::Instance()->GetResNum(); i++)
    {
        auto pDesc = FunctionNpcDesc::Instance()->GetDescByIndex(i);
        CHECK_NULL(0, pDesc);
        bool function_type_check = true;
        for (int j = 0; j < (int)pDesc->function.size(); j++)
        {
            if (pDesc->function[j].type == EN_FUNCTION_NPC_TYPE_SCENE_TRANS || pDesc->function[j].type == EN_FUNCTION_NPC_TYPE_YUNBIAO)
            {
                function_type_check = false;
                break;
            }
        }
        if (function_type_check)
        {
            continue;
        }

        if (pDesc->map_id != iMapDescID)
        {
            continue;
        }

        int distances = sqrt(abs((int64_t)(oPos.x - pDesc->x_axis) * (oPos.x - pDesc->x_axis)) + abs((int64_t)(oPos.z - pDesc->z_axis) * (oPos.z - pDesc->z_axis)));
        if (distances > 30000)
        {
            continue;
        }

        LOGSVR_TRACE("--check npc id:" << pDesc->id << " distances:" << distances << " oPos.x:" << oPos.x << " oPos.z:" << oPos.z << "--");

        return 1;
    }

    return 0;
}

int NFCityPlayerEscortAgent::UpdateMapData(NFCityPlayer* pPlayer, int iMapDescID, const Proto_Vector3& oPos, int iDirection)
{
    CHECK_NULL(0, pPlayer);
    do
    {
        if (!GetEscortState())
        {
            break;
        }

        if (CheckNearTeleports(iMapDescID, oPos) != 0)
        {
            break;
        }

        if (m_iMapDescID != iMapDescID)
        {
            m_bChangeMap = true;
        }

        if (m_iMapDescID == iMapDescID)
        {
            int iMove = sqrt((oPos.x - m_oPos.x) * (oPos.x - m_oPos.x) + (oPos.z - m_oPos.z) * (oPos.z - m_oPos.z));
            if (m_bChangeMap)
            {
                m_iChangeMapMove += iMove;
                if (m_iChangeMapMove > 30000)
                {
                    m_iChangeMapMove = 0;
                    m_bChangeMap = false;
                }
            }
            else
            {
                m_iMove += iMove;
            }
        }

        if (m_iMove > (uint32_t)ConstantDescEx::Instance()->GetValue(proto_ff::EN_CONST_ESCORT_FIGHT_RANGE) && m_iMapDescID == iMapDescID)
        {
            m_iMove = 0;

            proto_ff::Proto_CGEscortTriggerBattleNtf escort_trigger_battle_ntf;
            escort_trigger_battle_ntf.set_role_id(pPlayer->GetRoleID());

            NFCityPacketSendUtil::SendMsgToLogicServer(pPlayer, E_CG_ESCORT_TRIGGER_BATTLE_NTF, &escort_trigger_battle_ntf);
        }
    }
    while (false);

    m_iMapDescID = iMapDescID;
    m_oPos = oPos;
    m_iDirection = iDirection;

    return 0;
}
